package com.deke.baby.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.deke.baby.domain.BabyFamilyMember;
import com.deke.baby.mapper.BabyFamilyMemberMapper;
import com.deke.baby.vo.BabyFamilyVo;
import com.deke.common.utils.DateUtils;
import com.deke.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.deke.baby.mapper.BabyFamilyMapper;
import com.deke.baby.domain.BabyFamily;
import com.deke.baby.service.IBabyFamilyService;

/**
 * 家庭Service业务层处理
 *
 * @author deke
 * @date 2023-06-28
 */
@Service
public class BabyFamilyServiceImpl implements IBabyFamilyService
{
    @Autowired
    private BabyFamilyMapper babyFamilyMapper;

    @Autowired
    private BabyFamilyMemberMapper babyFamilyMemberMapper;

    /**
     * 查询家庭
     *
     * @param id 家庭主键
     * @return 家庭
     */
    @Override
    public BabyFamilyVo selectBabyFamilyById(Long id)
    {
        BabyFamilyVo vo=new BabyFamilyVo();
        vo.setBabyFamily(babyFamilyMapper.selectBabyFamilyById(id));
        BabyFamilyMember member=new BabyFamilyMember();
        member.setFamilyId(id);
        vo.setBabyFamilyMemberList(babyFamilyMemberMapper.selectBabyFamilyMemberList(member));
        return vo;
    }

    /**
     * 查询家庭列表
     *
     * @param babyFamily 家庭
     * @return 家庭
     */
    @Override
    public List<BabyFamily> selectBabyFamilyList(BabyFamily babyFamily)
    {

        List<BabyFamily> list=babyFamilyMapper.selectBabyFamilyList(babyFamily);
        return list;
    }

    List<BabyFamilyVo> memberList( List<BabyFamily> list){
        List<BabyFamilyVo> voList=new ArrayList<>();
        for (BabyFamily b:list) {
            BabyFamilyVo vo=new BabyFamilyVo();
            vo.setBabyFamily(b);
            BabyFamilyMember member=new BabyFamilyMember();
            member.setFamilyId(b.getId());
            vo.setBabyFamilyMemberList(babyFamilyMemberMapper.selectBabyFamilyMemberList(member));
            voList.add(vo);
        }
        return voList;
    }

    /**
     * 新增家庭
     *
     * @param vo 家庭
     * @return 结果
     */
    @Override
    public void insertBabyFamily(BabyFamilyVo vo)
    {
        BabyFamily babyFamily=  vo.getBabyFamily();
        babyFamily.setCreateTime(DateUtils.getNowDate());
        babyFamily.setCreateBy(SecurityUtils.getUserId().toString());
        babyFamilyMapper.insertBabyFamily(babyFamily);
        for (BabyFamilyMember m:vo.getBabyFamilyMemberList()) {
            m.setFamilyId(babyFamily.getId());
            babyFamilyMemberMapper.insertBabyFamilyMember(m);
        }
    }


    /**
     * 修改家庭
     *
     * @param vo 家庭
     * @return 结果
     */
    @Override
    public void updateBabyFamily(BabyFamilyVo vo)
    {
        BabyFamily babyFamily=  vo.getBabyFamily();
        babyFamily.setUpdateTime(DateUtils.getNowDate());
        babyFamily.setUpdateBy(SecurityUtils.getUserId().toString());
        babyFamilyMapper.updateBabyFamily(babyFamily);
        babyFamilyMemberMapper.deleteBabyFamilyMemberById(babyFamily.getId());
        for (BabyFamilyMember m:vo.getBabyFamilyMemberList()) {
            m.setFamilyId(babyFamily.getId());
            babyFamilyMemberMapper.insertBabyFamilyMember(m);
        }
    }

    /**
     * 批量删除家庭
     *
     * @param ids 需要删除的家庭主键
     * @return 结果
     */
    @Override
    public int deleteBabyFamilyByIds(Long[] ids)
    {

        return babyFamilyMapper.deleteBabyFamilyByIds(ids);
    }

    /**
     * 删除家庭信息
     *
     * @param id 家庭主键
     * @return 结果
     */
    @Override
    public int deleteBabyFamilyById(Long id)
    {
        return babyFamilyMapper.deleteBabyFamilyById(id);
    }
}
